//
//  ImageScrollView.m
//  scrollview2
//
//  Created by student on 16/2/29.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ImageScrollView.h"

@interface ImageScrollView ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pagecontroller;
@end

@implementation ImageScrollView

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)setImagenames:(NSArray *)imagenames
{
    _imagenames=imagenames;
    CGFloat imgwidth=self.scrollView.bounds.size.width;
    CGFloat imgheight=self.scrollView.bounds.size.height;
    for(int i=0;i<imagenames.count;i++)
    {
        UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(imgwidth*i, 0, imgwidth, imgheight)];
        imgview.image=[UIImage imageNamed:imagenames[i]];
        [self.scrollView addSubview:imgview];
    }
    self.pagecontroller.numberOfPages=imagenames.count;
}
-(void)setOthercolor:(UIColor *)othercolor
{
    _othercolor=othercolor;
    self.pagecontroller.pageIndicatorTintColor=othercolor;
}
-(void)setCurcolor:(UIColor *)curcolor
{
    _curcolor=curcolor;
    self.pagecontroller.currentPageIndicatorTintColor=curcolor;
}
-(id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if(self)
    {
        UIScrollView *scrollview=[[UIScrollView alloc]init];
        [self addSubview:scrollview];
        scrollview.pagingEnabled=YES;
        scrollview.showsHorizontalScrollIndicator=NO;
        self.scrollView=scrollview;
        self.scrollView.delegate=self;
        
        UIPageControl *pagecontrol=[[UIPageControl alloc]init];
        pagecontrol.numberOfPages=6;
        pagecontrol.currentPage=0;
        
        
        pagecontrol.pageIndicatorTintColor=[UIColor redColor];
        pagecontrol.currentPageIndicatorTintColor=[UIColor yellowColor];
        [self addSubview:pagecontrol];
        self.pagecontroller=pagecontrol;
        
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.frame=self.bounds;
    CGFloat imgwidth=self.scrollView.bounds.size.width;
    CGFloat imgheight=self.scrollView.bounds.size.height;
    for(int i=0;i<self.imagenames.count;i++)
    {
        UIImageView *imgview=self.scrollView.subviews[i];
        imgview.frame=CGRectMake(imgwidth*i, 0, imgwidth, imgheight);
    }
    self.scrollView.contentSize=CGSizeMake(imgwidth*self.imagenames.count, 0);
    CGFloat pageW=100;
    CGFloat pageH=20;
    CGFloat pageX=self.bounds.size.width/2-pageW/2;
    CGFloat pageY=self.bounds.size.height-pageH;
    self.pagecontroller.frame=CGRectMake(pageX, pageY, pageW, pageH);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pagecontroller.currentPage=scrollView.contentOffset.x/320;
}


@end
